package com.zznode.redsun.commons.sercurity.model;

import java.util.Collection;
import java.util.Vector;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import com.zznode.csp.domain.basemodel.DomainBusiness;

@Entity
@DynamicInsert
@DynamicUpdate
@Table(name = "s_account")
public class Account extends DomainBusiness {
	public static final String ADMIN_ID = "admin";

	private static final long serialVersionUID = 1L;

	@Column(name = "password")
	private String password;

	@Column(name = "enabled")
	private Boolean enabled;

	@Column(name = "user_id")
	private Long userId;

	@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
	@JoinTable(name = "s_account_role", inverseJoinColumns = { @JoinColumn(name = "role_id") }, joinColumns = { @JoinColumn(name = "account_id") })
	private Collection<Role> roles;

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Boolean getEnabled() {
		return enabled;
	}

	public void setEnabled(Boolean enabled) {
		this.enabled = enabled;
	}

	public Collection<Role> getRoles() {
		return roles;
	}

	public void setRoles(Collection<Role> roles) {
		this.roles = roles;
	}

	public void addRole(Role role) {
		if (null == role) {
			return;
		}
		if (null == this.roles) {
			this.roles = new Vector<Role>();
		}
		if (!this.roles.contains(role)) {
			this.roles.add(role);
		}
	}

	public void removeRole(Role role) {
		if (null == role || null == this.roles) {
			return;
		}
		if (this.roles.contains(role)) {
			this.roles.remove(role);
		}
	}
}
